package wuxianshua.schedul;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import wuxianshua.service.SpilderService;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.time.LocalDateTime;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * Description:
 * Datetime:    2021/3/9   21:14
 * Author:   jianglei
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SendEmail {

    @Autowired
    private SpilderService spilderService;

    @Autowired
    private RedisTemplate <String,String>redisTemplate;

    //3.添加定时任务
//    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
//    @Scheduled(fixedRate=5000)
    private void configureTasks() throws IOException, GeneralSecurityException, MessagingException {
        int spilder = spilderService.spilder();
        Boolean expirkey = redisTemplate.hasKey("expir");
        if(spilder >0 && !expirkey ){
            sendMessage();
            redisTemplate.opsForValue().set("expir","");
            redisTemplate.expire("expir",1, TimeUnit.HOURS);
        }else {
            System.err.println("冷却中:" + expirkey+"------有机器:"+(spilder>=1) );
        }
    }

    private   void  sendMessage() throws MessagingException, GeneralSecurityException {

        Properties props = new Properties();
        props.setProperty("mail.host","smtp.qq.com");

        props.setProperty("mail.transport.protocol","smtp");

        props.setProperty("mail.smtp.auth","true");

        //QQ存在一个特性设置SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);


        Session session = Session.getInstance(props);
        session.setDebug(true);

        Message msg = new MimeMessage(session);
        msg.setSubject("服务器有货了");
        msg.setText("服务器有货了，，，https://featurize.cn/dashboard/instances");
        msg.setFrom(new InternetAddress("1315745348@qq.com"));//发件人邮箱(我的163邮箱)
        msg.setRecipient(Message.RecipientType.TO,
                new InternetAddress("ms_jianglei@foxmail.com")); //收件人邮箱(我的QQ邮箱)
        msg.saveChanges();

        Transport transport = session.getTransport();
        transport.connect("1315745348@qq.com","hqwcpvihorqticcd");//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)

        transport.sendMessage(msg, msg.getAllRecipients());
        System.out.println("邮件发送成功...");
        transport.close();

    }
}